package com.youyou.fusheng2.View.Adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.youyou.fusheng2.Moudle.OnClick;
import com.youyou.fusheng2.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @name fusheng2
 * @class name：com.youyou.fusheng2.View.Adapter
 * @anthor 浮生 QQ:2567294605
 * @time 2020/8/25 00:54
 * @to
 */
public class SouSuoLiShiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<String> listItem;
    private List<String> listItem2 = new ArrayList<>();
    private List<String> tuijianList;
    private int     i;
    private OnClick onClick;

    public SouSuoLiShiAdapter(int i, List<String> listItem, OnClick onClick) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  02:05
         *  @简介: 传过来一个 i 根据此参数判断加载的那个布局
         *  如果listItem==0就给lietItem设置默认的历史数据
         */
        this.i = i;
        if (listItem.size() == 0) {
            listItem2.add("八佰");
            listItem2.add("我和我的家乡");
            listItem2.add("在爱我一点点");
            this.onClick = onClick;
        } else {
            this.listItem = listItem;
            this.onClick = onClick;
        }
    }

    public void setListItem(List<String> tuijianList) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  02:05
         *  @简介: 传过来一个 i 根据此参数判断加载的那个布局
         *  tuijianList==0就给lietItem设置默认的历史数据
         */
        if (tuijianList == null) {
            listItem2.add("八佰");
            listItem2.add("妈妈的朋友");
            listItem2.add("小姨子的黑森林");
        } else {
            this.tuijianList = tuijianList;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.sousuo_item, parent, false);
        return new SouSuoItem(inflate);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  02:08
         *  @简介: 根据i判断加载的布局
         */
        if (i == 1) {
            ((SouSuoItem) holder).sousuoitem.setText(listItem == null ? listItem2.get(position) : listItem.get(position));
        } else {
            ((SouSuoItem) holder).sousuoitem.setText(tuijianList == null ? listItem2.get(position) : tuijianList.get(position));
        }
        dainjishijian(((SouSuoItem) holder),position);
    }

    private void dainjishijian(SouSuoItem holder, int position) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/26  20:32
         *  @简介: 点击事件回掉
         */
       holder.sousuoitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClick.listener(holder.sousuoitem.getText().toString(), position);
            }
        });
    }

    @Override
    public int getItemCount() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  02:08
         *  @简介: 如果集合为null 就加载默认的集合长度
         */
        if (i == 1) {
            return listItem == null ? listItem2.size() : listItem.size();
        } else if (i == 2) {
            return tuijianList == null ? listItem2.size() : tuijianList.size();
        } else {
            return 0;
        }
    }

    private static class SouSuoItem extends RecyclerView.ViewHolder {

        private TextView sousuoitem;

        @SuppressLint("ResourceAsColor")
        public SouSuoItem(View inflate) {
            super(inflate);
            sousuoitem = inflate.findViewById(R.id.sousuoitem);
        }
    }
}
